<?php

declare(strict_types=1);

/**
 * 话费系统客服功能 在线表单提交 问题返回
 * User：Abell
 * Date：2020/4/17
 * Time：14:00
 */

namespace App\Controller\Admin;

use App\Constants\ErrorCode;
use App\Controller\AbstractController;
use App\Utils\ApiUtils;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Di\Annotation\Inject;
use App\Service\WxserviceService;
use App\Validate\WxserviceValidation;

class WxserviceController extends AbstractController{

    /**
     * @Inject
     * @var WxserviceService
     */
    private $service;

    /**
     * 基本的表单提交客服信息列表查看 问题反馈列表
     * @Author abell
     * @DateTime 2020-05-28
     * @param array $request
     * @return object
     */
    public function index(RequestInterface $request){
        $data = $this->service->formList($request->all());
        return  $this->response->json(ApiUtils::send($data['code'],$data['msg'],$data['data'],$data['total']));
    }

    /**
     * 基本的表单提交客服信息列表查看和编辑 问题反馈列表编辑处理
     * @Author abell
     * @DateTime 2020-05-28
     * @param array $request
     * @return object
     */
    public function edit(RequestInterface $request){

        if($request->isMethod('get')){
            make(WxserviceValidation::class)->check($request->all(),'admin_edit')->response();
            $result = $this->service->getForm($request->all());
        }else{
            make(WxserviceValidation::class)->check($request->all(),'admin_edit_submit')->response();
            $result = $this->service->editForm($request->all());
        }
        return  $this->jsonApi($result);
    }

//-------------------------------------------------------------


    /**
     * 新增问题分类
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function classifyAdd()
    {
        $title = $this->request->input('title');
        $operator = $this->request->input('operator');
        make(WxserviceValidation::class)->check(['title'=>$title],'classifyAdd')->response();
        $result = $this->service->classifyAdd($title, $operator);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'添加成功',$result));
    }

    /**
     * 获取全部问题分类
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function classifyAll()
    {
        $result = $this->service->classifyAll();
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'查询成功',$result));
    }

    /**
     * 编辑问题分类
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function classifyEdit()
    {
        $id = $this->request->input('id');
        if($this->request->isMethod('post')){
            //接收参数
            $title = $this->request->input('title');
            $operator = $this->request->input('operator');
            make(WxserviceValidation::class)->check(['title'=>$title, 'id'=>$id],'classifyEdit_post')->response();
            $result = $this->service->classifyEdit($id, $title, $operator);
            return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'编辑成功',$result));

        }
        make(WxserviceValidation::class)->check(['id'=>$id],'classifyEdit_get')->response();
        $result = $this->service->classifyGet($id);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$result));
    }

    /**
     * 删除问题分类
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function classifyDel()
    {
        $id = $this->request->input('id');
        make(WxserviceValidation::class)->check(['id'=>$id],'classifyDel')->response();
        $result = $this->service->classifyDel($id);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'操作成功',$result));
    }

    /**
     * 添加问题类型
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function typeAdd()
    {
        $data['wxservice_ope_id'] = $this->request->input('wxservice_ope_id');
        $data['title'] = $this->request->input('title');
        make(WxserviceValidation::class)->check($data,'typeAdd')->response();
        $result = $this->service->typeAdd($data);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'添加成功',$result));
    }

    /**
     * 获取全部问题类型
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function typeAll()
    {
        $result = $this->service->typeAll();
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'查询成功',$result));
    }

    /**
     * 编辑问题类型
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function typeEdit()
    {
        $id = $this->request->input('id');
        if($this->request->isMethod('post')){
            //接收参数
            $data['id'] = $id;
            $data['wxservice_ope_id'] = $this->request->input('wxservice_ope_id');
            $data['title'] = $this->request->input('title');
            make(WxserviceValidation::class)->check($data,'typeEdit_post')->response();
            $result = $this->service->typeEdit($data);
            return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'编辑成功',$result));

        }
        make(WxserviceValidation::class)->check(['id'=>$id],'typeEdit_get')->response();
        $result = $this->service->typeGet($id);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$result));
    }

    /**
     * 删除问题分类
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function typeDel()
    {
        $id = $this->request->input('id');
        make(WxserviceValidation::class)->check(['id'=>$id],'typeDel')->response();
        $result = $this->service->typeDel($id);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'操作成功',$result));
    }

    /**
     * 反馈数据排行列表数据
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function countRank(){
        $pageSize = $this->request->input('pageSize');
        $result = $this->service->countRankList($pageSize);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$result['list'], $result['count']));
    }

    /**
     * 反馈数据类型排行列表数据
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function countTypeRank(){
        $pageSize = $this->request->input('pageSize');
        $result = $this->service->countTypeRankList($pageSize);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$result['list'], $result['count']));
    }
}